//
//  NeedAuthenVC.swift
//  DianXia
//
//  Created by you&me on 2017/12/18.
//  Copyright © 2017年 you&me. All rights reserved.
//

import UIKit
class NeedAuthenCell: UITableViewCell {
    @IBOutlet weak var cellTitle: UILabel!
    @IBOutlet weak var cellOKBtn: UIButton!
    @IBOutlet weak var cellNoBtn: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}


class NeedAuthenVC: UIViewController {

    @IBOutlet weak var alertImage: UIImageView!
    @IBOutlet weak var alertLabel: UILabel!
    @IBOutlet weak var needTabView: UITableView!
    @IBOutlet weak var backBtn: UIButton!

    let needVM = HomeVM.shareHomeVN
    let dispose = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        needTabView.tableFooterView = UIView()
        events()
    }

    fileprivate func events() {
        
        needVM.modelList.asObservable()
              .subscribe(onNext: { (listModels) in
                self.showAlertState(listModels)
               })
              .disposed(by: dispose)

        // 返回按钮的点击事件
        backBtn.rx.tap
            .subscribe({ [weak self] _ in self?.dismiss(animated: true, completion: nil) })
            .disposed(by: dispose)
    }


    /// 根据获取的数据跟新界面
    ///
    /// - Parameter listModels: 数据model
    fileprivate func showAlertState(_ listModels:[ListModel] ) {
        if listModels.count > 0 {
            needTabView.isHidden = false
            needTabView.reloadData()
            alertImage.isHidden = true
            alertLabel.isHidden = true
        } else {
            needTabView.isHidden = true
            alertImage.isHidden = false
            alertLabel.isHidden = false
        }
    }

}

extension NeedAuthenVC: UITableViewDelegate, UITableViewDataSource {
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return needVM.modelList.value.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NeedAuthenCell", for: indexPath) as! NeedAuthenCell
        let model = needVM.modelList.value[indexPath.row]
        cell.cellTitle.text = "姓名：\(model.name ?? "") 手机号：\(model.mobile ?? ""), 正在提交审批，是否同意?"
        cell.cellOKBtn.rx.tap
            .subscribe({ [weak self] _ in self?.needVM.authenResult(model.mobile ?? "", "yes", indexPath.row) })
            .disposed(by: dispose)
        cell.cellNoBtn.rx.tap
            .subscribe({ [weak self] _ in self?.needVM.authenResult(model.mobile ?? "", "no", indexPath.row) })
            .disposed(by: dispose)
        return cell
    }
}

